﻿using DataLayer;
using DataLayer.BD;
using SupportLayer.Constantes.Tabelas;
using SupportLayer.Objetos.Outros;
using SupportLayer.ExtensionMethods;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SupportLayer.Constantes.OUTROS;

namespace BusinessLayer
{
	public class Business
	{
		private Dictionary<string, object> parametros;

		#region Banner
		public int InsertBanner(BannerClass _banner)
		{
			return new DataAccess().Insert(_banner, BANNER.ID, TABELAS.BANNER);
		}
		public DataTable CarregaBanner(string _dataField)
		{
			try
			{
				//DataTable dt = new DataTable();

				return new DataAccess().Select(new BannerClass(), TABELAS.BANNER, new string[] { "*" }, null);







				//dt.Columns.Add(_dataField);

				//foreach (BannerClass banner in Control.GetBanners())
				//{
				//	dt.Rows.Add("/Imagens/Banner/" + banner.ImageUrl);
				//}






				//return dt;
			}
			catch (Exception)
			{

				throw;
			}
		}
		public List<BannerClass> GetBanners()
		{
			try
			{
				return new DataAccess().Select(new BannerClass(), TABELAS.BANNER, new string[] { "*" }, null).toBannerClass();
			}
			catch (Exception)
			{
				throw;
			}
		}
		public void BannerChangeOrders(int bannerID, bool goingUp)
		{
			DataAccess da = new DataAccess();

			parametros = new Dictionary<string,object>();
			parametros.Add(BANNER.ID, bannerID);

			var lstbanner = da.Select(new BannerClass(), TABELAS.BANNER, new string[] { "*" }, null).toBannerClass();

			if (lstbanner == null)
				return;

			lstbanner = lstbanner.OrderBy(a => a.Ordem).ToList();

			for (int i = 0; i < lstbanner.Count; i++)
			{
				if (lstbanner[i].ID == bannerID)
				{
					BannerClass banner = lstbanner[i];
					BannerClass bannerAfetado = new BannerClass();
					Int16 pos = banner.Ordem;
					Int16 posAfetado = 0;

					if(goingUp)
					{
						bannerAfetado = lstbanner[i - 1];
						posAfetado = bannerAfetado.Ordem;
						bannerAfetado.Ordem = pos;
						banner.Ordem = posAfetado;
					}
					else
					{
						bannerAfetado = lstbanner[i + 1];
						posAfetado = bannerAfetado.Ordem;
						bannerAfetado.Ordem = pos;
						banner.Ordem = posAfetado;
					}

					Dictionary<string, object> set = new Dictionary<string, object>();
					set.Add(BANNER.ORDEM, banner.Ordem);
					parametros = new Dictionary<string, object>();
					parametros.Add(BANNER.ID, banner.ID);
					da.Update(banner, TABELAS.BANNER, set, parametros);



					set = new Dictionary<string, object>();
					set.Add(BANNER.ORDEM, bannerAfetado.Ordem);
					parametros = new Dictionary<string, object>();
					parametros.Add(BANNER.ID, bannerAfetado.ID);
					da.Update(bannerAfetado, TABELAS.BANNER, set, parametros);

					break;
				}
			}
		}
		public int BannerGetLast()
		{
			return Convert.ToInt32(new DataAccess().GetIdentity(TABELAS.BANNER, BANNER.ID));
		}
		public void DeleteBanner(int bannerID)
		{
			new DataAccess().Delete(TABELAS.BANNER, BANNER.ID, bannerID);
		}
		#endregion
		#region Contato
		public int InsertContato(ContatoClass Contato)
		{
			return new DataAccess().Insert(Contato, CONTATO.ID, TABELAS.CONTATO);
		}
		public List<ContatoClass> GetContatos()
		{
			try
			{
				return new DataAccess().Select(new ContatoClass(), TABELAS.CONTATO, new string[] { "*" }, null).toContatoClass();
			}
			catch (Exception)
			{
				throw;
			}
		}
		#endregion
	}
}
